/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <cmsis_os2.h>
//#include "softbus_server_frame.h"
//#include "auth_interface.h"
#include "bus_center_manager.h"
//#include "lnn_bus_center_ipc.h"
//#include "message_handler.h"
//#include "softbus_conn_interface.h"
//#include "softbus_disc_server.h"
#include "softbus_errcode.h"
//#include "softbus_feature_config.h"
//#include "softbus_log.h"
//#include "softbus_utils.h"
//#include "trans_session_manager.h"
#include "trans_session_service.h"
#include "session.h"


#define CONFIG_TASK_MAIN_PRIOR 25

#include <pthread.h>
#include "ohos_init.h"

extern void testThread(void(*test) (void *arg));
extern void hi_wifi_set_macaddr(char *mac, int len);

#include <cmsis_os2.h>
#include "discovery_service.h"
#include "session.h"
#include "iot_wifi.h"

#define g_demoModuleName  "discovered_demo_zyh"
#define g_demoSessionName  "zyh_test_softbus_1_27"
#define CONFIG_TASK_MAIN_PRIOR 25

int32_t g_sessionId = -1;

void OnSuccess(int publishId) {
    printf("pushlish OnSuccess\n");
    //TBD
}

void OnFail(int publishId, PublishFailReason reason) {
    //TBD
    printf("publishId :%d , Faied error :%d\n", publishId, reason);
}

int TestOnSessionOpened(int sessionId, int result) {
    g_sessionId = sessionId;
    printf("TestOnSessionOpened %d, %d \r\n", g_sessionId, result);
    return 0;
}

int TestOnSessionClosed(int sessionId, int result) {
    
    printf("TestOnSessionClosed\n");
    
    if(g_sessionId == sessionId) {
        g_sessionId = -1;
    }

    return 0;
}

void TestOnBytesReceived(int sessionId, const void *data, unsigned int dataLen) {

    printf("BytesReceived :data  %s, len: %d \r\n", data, dataLen);
  
}

void TestOnMessageReceived(int sessionId, const void *data, unsigned int dataLen) {

    printf("MessageReceived :data  %s, len: %d \r\n", data, dataLen);
}

static PublishInfo g_publishInfo = {
    .capabilityData = (unsigned char *)"1",
    .capability = "ddmpCapability",
    .dataLen = 1,
    .publishId = 1,
    .mode = DISCOVER_MODE_ACTIVE,
    .medium = COAP,
    .freq = MID,
};

static IPublishCallback g_publishCallback = {
    .OnPublishSuccess = OnSuccess,
    .OnPublishFail = OnFail,
};

static ISessionListener g_sessionCallback = {
    .OnSessionOpened = TestOnSessionOpened,
    .OnSessionClosed = TestOnSessionClosed,
    .OnBytesReceived = TestOnBytesReceived,
    .OnMessageReceived = TestOnMessageReceived,
};


static void InitSoftbus(void) {
    if (BOARD_ConnectWifi("zhangyuhang", "12345678") != 0) {
        printf("Connect wifi failed \n");
        return;
    }
    int ret = -1;
    printf(">>>>>%d:%s()\n", __LINE__, __FUNCTION__);

    InitSoftBusServer();
    printf(">>>>>%d:%s()\n", __LINE__, __FUNCTION__);
    
    int testCount = 0;
    int maxTestCount = 5;
    while (testCount < maxTestCount) {
        ret = PublishService(g_demoModuleName, &g_publishInfo, &g_publishCallback);
        if (ret != 0) {
            printf("PublishService err\n");
        }
        testCount++;
    }

    sleep(5);  // TBD
    
    ret = CreateSessionServer(g_demoModuleName, g_demoSessionName, &g_sessionCallback);
    if (ret != 0) { 
        printf("CreateSessionServer err\n");
    }
        
    printf("InitSoftbus ok\n");
}

void SoftbusDiscoveredTestEntry() {
    printf("Enter SoftbusTestEntry \n");
    char mac[6] = {0x8c, 0x22, 0x33, 0x44, 0x55, 0x33};
    hi_wifi_set_macaddr(mac, sizeof(mac));
    osThreadAttr_t attr;
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 8096;
    attr.priority = CONFIG_TASK_MAIN_PRIOR;

    attr.name = "SoftbusTest";
    (void) osThreadNew((osThreadFunc_t)InitSoftbus, NULL, (const osThreadAttr_t *)&attr);

    return;
}

APP_FEATURE_INIT(SoftbusDiscoveredTestEntry);
